package com.li.link;

public class SingleLinkedListdemo1 {
    public static void main(String[] args) {
        //测试
        HereNode hereNode1 = new HereNode(1, "宋江", "及时雨");
        HereNode hereNode2 = new HereNode(2, "卢俊义", "玉麒麟");


        HereNode hereNode3 = new HereNode(3, "吴用", "智多星");
        HereNode hereNode4 = new HereNode(4, "林冲", "豹子头");
        Singlelelist singlelelist = new Singlelelist();
        //singlelelist.add(hereNode1);
        // singlelelist.add(hereNode2);
        //singlelelist.add(hereNode3);
        //singlelelist.add(hereNode4);
        //singlelelist.list();

        singlelelist.addbyorder(hereNode1);
        singlelelist.addbyorder(hereNode4);
        singlelelist.addbyorder(hereNode2);
        singlelelist.addbyorder(hereNode3);
        singlelelist.list();
        reversLink(singlelelist.getHead());
        singlelelist.list();



    }

    public static void reversLink(HereNode head) {
        
        if (head.next == null || head.next.next == null) {
            return;
        }
        //辅助指针，帮忙遍历原先链表
        HereNode cur = head.next;
        HereNode next = null;
        HereNode reverse = new HereNode(0, "", "");
        while (cur != null) {
            next = cur.next;
            cur.next = reverse.next;
            reverse.next = cur;
            cur = next;
        }
        head.next = reverse.next;
    }

    //查找第k个节点
    //把链表遍历，得到总长度size
    //从第一个开始遍历
    public static HereNode finLastIndex(HereNode hereNode, int index) {
        if (hereNode.next == null) {
            return null;
        }
        int size = getLenth(hereNode);
        //第二次遍历size-index位置，就是我们倒数第k个节点
        if (index <= 0 || index > size) {
            return null;
        }
        //辅助变量
        HereNode cur = hereNode.next;
        for (int i = 0; i < size - index; i++) {
            cur = cur.next;

        }
        return cur;
    }

    public static int getLenth(HereNode head) {
        if (head.next == null) {
            return 0;
        }
        int lenth = 0;
        HereNode cu = head.next;//辅助变量
        while (cu != null) {
            lenth++;
            cu = cu.next;
        }
        return lenth;
    }
}

class Singlelelist {
    //头节点
    private HereNode head = new HereNode(0, "", "");

    public HereNode getHead() {
        return head;
    }

    public void add(HereNode hereNode) {
        HereNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = hereNode;
    }

    public void addbyorder(HereNode hereNode) {
        //复制节点temp，位于添加位置前一个结点
        HereNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {//temp到链表最后
                break;
            }
            if (temp.next.no > hereNode.no) {//位置找到
                break;
            } else if (temp.next.no == hereNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("准备插入编号%d 已经存在，不能加入", hereNode.no);
        } else {
            hereNode.next = temp.next;
            temp.next = hereNode;
        }

    }

    //根据编号修改
    public void update(HereNode hereNode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HereNode temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == hereNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = hereNode.name;
            temp.nickname = hereNode.nickname;

        } else {
            System.out.printf("没有找到%d", hereNode.no);
        }
    }

    public void delete(int no) {
        HereNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.printf("删除的%d节点不存在\n", no);
        }
    }

    //显示链表
    public void list() {
        if (head.next == null) {
            System.out.println("链表空");
            return;
        }
        HereNode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

//定义一个节点
class HereNode {
    public int no;
    public String name;
    public String nickname;
    public HereNode next;

    public HereNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;

    }

    @Override
    public String toString() {
        return "HereNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +

                '}';
    }
}
